package com.lzb.compressmaster.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.GoCompressTabAdapter;
import com.lzb.compressmaster.bean.GoCompressTabBean;
import com.lzb.compressmaster.databinding.ActivityGoCompressBinding;
import com.lzb.compressmaster.fragment.AudioFragment;
import com.lzb.compressmaster.fragment.DocumentFragment;
import com.lzb.compressmaster.fragment.PictureFragment;
import com.lzb.compressmaster.fragment.VideoFragment;
import com.lzb.compressmaster.utils.CompressFileManager;

import java.util.ArrayList;
import java.util.List;

public class GoCompressActivity extends BaseAc<ActivityGoCompressBinding> implements OnItemClickListener, View.OnClickListener {

    private List<Fragment> mFragmentList;
    private GoCompressTabAdapter mTabAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_go_compress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGoCompressBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvGoCompress.setOnClickListener(this);

        mDataBinding.rvGoCompressTab.setLayoutManager(new GridLayoutManager(mContext, 4));
        mTabAdapter = new GoCompressTabAdapter();
        mDataBinding.rvGoCompressTab.setAdapter(mTabAdapter);
        mTabAdapter.setOnItemClickListener(this);

        mFragmentList = new ArrayList<>();
        mFragmentList.add(new PictureFragment());
        mFragmentList.add(new VideoFragment());
        mFragmentList.add(new AudioFragment());
        mFragmentList.add(new DocumentFragment());
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flFragment, 0);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvGoCompress) {//去压缩
            if (CompressFileManager.getInstance().getSelectFileBeans().size() == 0) {
                ToastUtils.showShort(R.string.please_sel_file);
            } else {
                FileCompressActivity.listPath = CompressFileManager.getInstance().getSelectFileBeans();
                startActivity(FileCompressActivity.class);
            }
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mTabAdapter.getItem(oldPosition).setSelected(false);
        mTabAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        mTabAdapter.getItem(position).setSelected(true);
        mTabAdapter.notifyItemChanged(position);
        FragmentUtils.showHide(position, mFragmentList);
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));

        String[] title = getResources().getStringArray(R.array.go_compress_tab_title);
        List<GoCompressTabBean> listTab = new ArrayList<>();
        listTab.add(new GoCompressTabBean(title[0], true));
        listTab.add(new GoCompressTabBean(title[1], false));
        listTab.add(new GoCompressTabBean(title[2], false));
        listTab.add(new GoCompressTabBean(title[3], false));
        mTabAdapter.setList(listTab);
    }

    public void refreshDataCount(CompressFileManager compressFileManager) {
        List<String> selectFileBeans = compressFileManager.getSelectFileBeans();
        if (CollectionUtils.isEmpty(selectFileBeans)) {
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        } else {
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, selectFileBeans.size()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!CompressFileManager.getInstance().getSelectFileBeans().isEmpty()) {
            CompressFileManager.getInstance().clearSelectFile();
        }
    }
}